<template>
  <view>
    <view  v-for="(item,index) in goodsList" :key="index">
      <view class="goods-item" @click="gotoGoodsDetail(item)">
        <my-goods :goods="item"></my-goods>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        queryObj: {
          cid: '',
          query: '',
          pagenum: '1',
          pagesize: 10
        },
        goodsList: [],
        defaultImg: 'https://file.moyublog.com/d/file/2022-03-15/x4hi3xasuoq.jpg',
        total: 0,
        isLoading: false
      }
    },
    onLoad(options) {
      this.queryObj.cid = options.cid || ''
      this.queryObj.query = options.query || ''
      this.getGoodsList()

    },
    methods: {
      async getGoodsList(cb) {
        this.isLoading = true
        // 3.1 发起请求
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/goods/search', this.queryObj)
        if (res.meta.status !== 200) {
          return uni.$showMsg()
        }
        cb && cb()
        this.goodsList = [...this.goodsList, ...res.message.goods]
        this.total = res.message.total
        this.isLoading = false
      },
      gotoGoodsDetail(item) {
        uni.navigateTo({
          url: '/subpackges/goods_detail/goods_detail?goods_id=' + item.goods_id
        })
      }
    },
    onReachBottom() {
      if (this.isLoading) return
      if (this.queryObj.pagesize * this.queryObj.pagenum >= this.total) return uni.$showMsg('数据加载完毕!')
      this.queryObj.pagenum++
      this.getGoodsList()
    },
    onPullDownRefresh() {
      this.queryObj.pagenum = 1
      this.total = 0
      this.goodsList = []
      this.isLoading = false
      this.getGoodsList(() => uni.stopPullDownRefresh())
    },

  }
</script>

<style lang="scss">
  .goods_list {
    .goods_list-item {
      padding-bottom: 5px;
      border: 2px solid #efefef;
      display: flex;

      // justify-content: space-between;
      .goods_list-item-left-box {
        padding-left: 5px;

        image {
          width: 100px;
          height: 100px;
          display: block;
        }
      }

      .goods_list-item-right-box {
        padding: 0px 5px;
        display: flex;
        justify-content: space-between;
        flex-direction: column;

        .goods-name {
          font-size: 12px;
        }

        .goods-price {
          font-size: 18px;
          color: #c00000;
        }
      }
    }
  }
</style>